package com.v2future.sjt.collegecheckingattendance.event.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.v2future.sjt.collegecheckingattendance.MainActivity;
import com.v2future.sjt.collegecheckingattendance.R;
import com.v2future.sjt.collegecheckingattendance.event.bean.OneEventBean;
import com.v2future.sjt.collegecheckingattendance.utils.WidgetUtil;
import com.v2future.sjt.collegecheckingattendance.widget.GridViewInScroll;

import org.xutils.x;

import java.util.List;

/**
 * Created by Administrator on 2016/1/15.
 */
public class EventListAdapter extends BaseAdapter {

    List<OneEventBean> dataList;
    Context mContext;

    public EventListAdapter(Context context, List<OneEventBean> dataList) {
        mContext = context;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        OneEventBean bean = dataList.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.listitem_event, null);
            holder = new ViewHolder();
            holder.iconIv = (ImageView) convertView.findViewById(R.id.iv_icon);
            holder.nameTv = (TextView) convertView.findViewById(R.id.tv_name);
            holder.collegeTv = (TextView) convertView.findViewById(R.id.tv_college);
            holder.timeTv = (TextView) convertView.findViewById(R.id.tv_time);
            holder.contentTv = (TextView) convertView.findViewById(R.id.tv_content);
            holder.imageGv = (GridViewInScroll) convertView.findViewById(R.id.gv_images);
            holder.hotTv = (TextView) convertView.findViewById(R.id.tv_hot);
            holder.tv_scan_count = (TextView) convertView.findViewById(R.id.tv_scan_count);
            holder.tv_join_count = (TextView) convertView.findViewById(R.id.tv_join_count);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //头像
        if (!TextUtils.isEmpty(bean.getUserPic())) {
            x.image().bind(holder.iconIv, bean.getUserPic(), ((MainActivity)mContext).getCircleImageOptions(50, 50));
//            x.image().bind(holder.iconIv, bean.getUserPic());
        }
        else {
            ((MainActivity)mContext).setDefaultHead(holder.iconIv);
        }
        //名字
        if (!TextUtils.isEmpty(bean.getUserName())) {
            holder.nameTv.setText(bean.getUserName());
        }
        //学院
        if (!TextUtils.isEmpty(bean.getSponsor())) {
            holder.collegeTv.setVisibility(View.VISIBLE);
            holder.collegeTv.setText(bean.getSponsor());
        }
        else {
            holder.collegeTv.setVisibility(View.GONE);
        }
        //内容
        if (!TextUtils.isEmpty(bean.getActionDesc())) {
            holder.contentTv.setText(bean.getActionDesc());
        }
        //时间
        if (!TextUtils.isEmpty(bean.getActionTime())) {
            holder.timeTv.setText(bean.getActionTime());
        }
        //热度
        if (!TextUtils.isEmpty(bean.getFavorateNum())) {
            holder.hotTv.setText(bean.getFavorateNum());
        }
        //图片
        if (null != bean.getImageUrl() && bean.getImageUrl().size() > 0) {
            holder.imageGv.setVisibility(View.VISIBLE);
            EventImgGridAdapter imageUrlAdapter = new EventImgGridAdapter(mContext, bean.getImageUrl());
            holder.imageGv.setAdapter(imageUrlAdapter);
        }
        else {
            holder.imageGv.setVisibility(View.GONE);
        }
        //扫码人数
        WidgetUtil.setText(holder.tv_scan_count, bean.getAllCheckedNumber());
        //参加人数
        WidgetUtil.setText(holder.tv_join_count, bean.getAllInvitedNumber());
        return convertView;
    }

    public class ViewHolder {
        ImageView iconIv;
        TextView nameTv;
        TextView collegeTv;
        TextView timeTv;
        TextView contentTv;
        GridViewInScroll imageGv;
        TextView hotTv;
        TextView tv_scan_count;
        TextView tv_join_count;
    }
}
